<?php
/**
 * ExtJS 4 Wrapper Package
 * @author Kirill A Egorov 2011
 *
 */
class Ext
{
	/**
	 * Create object
	 * @param string $class
	 * @param array $config - optional, object properties
	 * @throws Exception
	 * @return Ext_Object
	 */
	static public function create($class , array $config = array())
	{
		$class = self::translateClass($class);
		$propertyClass =  self::getPropertyClass($class);
		
		if(!class_exists($propertyClass))
			throw new Exception('Undefined properties for class ' . $class);
		
		if(class_exists($class))
			$object = new $class();
		else
			$object = new Ext_Object_Virtual($class);
			
		if(!empty($config))
			foreach ($config as $k=>$v)
				$object->$k = $v;		

		return $object;		
	}
	/**
	 * Translate classname from ExtJS into the wrapper classname
	 * @param string $name
	 * @return string
	 */
	static public function translateClass($name){
		return implode('_', array_map('ucfirst' , explode('.', $name)));
	}
	/**
	 * Calc Property classname by Object class
	 * @param string $class - Ext object class
	 * @throws Exception
	 * @return string
	 */
	static public function getPropertyClass($class)
	{		
		return 	'Ext_Property_' . $class;
	}
}